<script setup lang='ts'>
  import { onMounted, onUpdated, reactive } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import { $router, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  const action = reactive({
    total:0,
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis')),
  })
  const listQuery = reactive({
    user_id:'',
    page:1,
    limit:20,
    pool_level:0
  })
  const handleSerach = () => {
    getList();
  };
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await luckDrawApi.wishLuckDrawList({
      date_st,
      date_ed,
      ...listQuery
    })
    loading.close()
    const { list=[], total=0 } = res?.data
    action.list = list
    action.total = total
  }
  let primarySet = false
  const getPoolLevel = async()=>{
    if(primarySet) return
    primarySet = true
    const { pool_level=0 } = $router().currentRoute.value?.meta || { pool_level:0 }
    if(listQuery.pool_level != pool_level && pool_level) {
      listQuery.pool_level = pool_level as number
      const loading = Toastloading()
      action.list = []
      await getList()
      primarySet = false
      loading.close()
    }
  }
  const updatePoolLevel = ()=>{
    primarySet = false
    getPoolLevel()
  }
  onMounted(()=>{
    updatePoolLevel()
  })
  onUpdated(()=>{
    updatePoolLevel()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="order_id" label="订单ID" />
      <el-table-column min-width="80" prop="user_id" label="用户ID" />
      <el-table-column min-width="80" prop="uuid" label="用户UUID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column min-width="80" prop="gift_id" label="礼物ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="giftName" label="礼物名称" />
      <el-table-column min-width="80" prop="gift_num" label="礼物数量" />
      <el-table-column min-width="80" prop="prop_num" label="消耗许愿卡数量" />
      <el-table-column min-width="80" prop="prop_diamond_num" label="消耗许愿卡价值钻石数" />
      <el-table-column min-width="80" prop="reward_diamond_num" label="奖励价值钻石数" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>